let db  =require('../db');

//判断设备是否存在

exports.validationEquipment =  async function (mac) {
    let sql=`
    
    SELECT *FROM equipment  WHERE mac=:mac AND isdelete ='n'
    `
    let data  =await db.query(sql,{mac});
    return data[0];
}

//获取图片列表

exports.imagesList  = async function(){
    let sql=`
    SELECT 
    id, 
    name,
    url,
    departmentId AS unitId,
    isdelete,
    createdAt,
    updatedAt
    FROM  employees  WHERE  isdelete ='n'

    `
    let data   = await  db.query(sql);
    return data[0];
}

//获取通知公告的列表

exports.noticeList  = async function(){
 let  sql=`
 SELECT 
  id,
  title,
  content,
  img,
  noticeType,
 DATE_FORMAT( createdAt,'%Y-%m-%d %H:%i') AS createdAt
FROM 
notice WHERE isdelete='n'
ORDER BY createdAt DESC 
 `
 let  data  = await db.query(sql);
 return data[0];

}

//更改设备同步后的图片状态
exports.updatedImagesStatus = async  function(id){
    let sql=`
    UPDATE  employees  SET  synchronous ='y' WHERE  id =:id

    `
    let data   = await db.query(sql,{id});
    return data[0];
}


//获取设备掉线后所有的图像操作记录

exports.getRecoverList  = async function(unitId,lastDisconnectTime){
let sql=`
SELECT 
id, 
name,
url,
isdelete,
unitId
FROM  family  WHERE unitId =:unitId AND updatedAt>:lastDisconnectTime

`
let  data  =await db.query(sql,{unitId,lastDisconnectTime});
return data[0];
}

//获取图片数据集最新一条数据的时间

exports.getImagesNewstime  = async  function(){
    let sql=`
    SELECT *FROM  operation_log   WHERE   action IN ('imageAdd','imageDel','imageUpdate')  ORDER BY  id  DESC   LIMIT 1
    `
    let data  = await db.query(sql);
    return data[0];
}
